﻿#pragma once
#include <stdint.h>
#include <string>
#include <map>
using namespace std;

/*
// 通用排行榜Key
struct CacheMiscRankKey
{
	0 optional unsigned int		iObjId;//一般是玩家角色唯一id
	1 optional unsigned int 		iTime;//插入时间
	2 optional long				iRankParam1;//排名参数
	3 optional long				iRankParam2;//排名参数
	10 optional unsigned int	iExtraParam1;
	11 optional string			sExtraParam;
};
key[CacheMiscRankKey, iRankParam1, iRankParam2, iTime, iObjId];

// 基础通用排行
struct CacheMiscBaseRank
{
	0 optional map<CacheMiscRankKey, unsigned int> mRank;
};
*/

struct RankData
{
	RankData() : iRankId(0), iObjId(0)
	{

	}

	RankData(uint32_t iRankId_, uint32_t iObjId_, const JceData::CacheMiscRankKey& stRankKey_) : iRankId(iRankId_), iObjId(iObjId_), stRankKey(stRankKey_)
	{}

	uint32_t					iRankId;//排名
	uint32_t					iObjId;//一般是玩家角色唯一id
	JceData::CacheMiscRankKey	stRankKey;
};


class RankBaseManager
{
public:
	RankBaseManager(uint32_t iRankType, uint32_t iMiscType, uint32_t iRankMaxNum);
	virtual ~RankBaseManager();

public:
	// 获取排行MiscKey
	//virtual string getMiscKey();

	// 获取排行数据存储
	virtual map<JceData::CacheMiscRankKey, uint32_t>& getRankMap(uint32_t iSubId);

	// 检查是否并列
	virtual bool checkRankKeyTied(const JceData::CacheMiscRankKey &stKey1, const JceData::CacheMiscRankKey &stKey2);

	// 更新排名, 先自动删除旧排名在增加新排名
	virtual void updateRank(uint32_t iSubId, uint32_t iObjId, JceData::CacheMiscRankKey& stRankKey);

	// 拉取排行榜
	virtual void getRankList(uint32_t iSubId, uint32_t iStartRank, uint32_t iRankNum, vector<RankData>& vRankData);

	// 拉取并列排行榜
	virtual void getTiedRankList(uint32_t iSubId, uint32_t iMaxRankId, uint32_t iMaxRankNum, vector<RankData>& vRankData);

	// 获取自己排名
	virtual RankData getSelfRank(uint32_t iSubId, uint32_t iObjId);

	// 获取自己并列排名
	virtual RankData getTiedSelfRank(uint32_t iSubId, uint32_t iObjId);

	// 从排行榜中删除
	virtual void delFromRank(uint32_t iSubId, uint32_t iObjId);

	// 清空排行榜
	virtual void clearRankList();

	// 清空指定排行榜,只在subrank中实现
	virtual void clearSubRankList(uint32_t iSubId) {};

	JceData::CacheMisc& getCacheMisc()
	{
		return MISC_INFO_MGR->getCacheMisc(getMiscKey());
	}

	void setCacheMisc(JceData::CacheMisc& stCacheMisc)
	{
		MISC_INFO_MGR->setCacheMisc(getMiscKey(), stCacheMisc);
	}

public:
	uint32_t _iRankType;					// 排行榜类型
	uint32_t _iMiscType;					// Misc存储类型
	uint32_t _iRankMaxNum;					// 排行榜最大数量
};